package com.yyge.web.servlet;
import com.yyge.dto.EmployeeDto;
import com.yyge.entity.Employee;
import com.yyge.service.EmployeeService;
import com.yyge.service.impl.EmployeeServiceImpl;
import com.yyge.util.ServletUtil;
import com.yyge.vo.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;

@WebServlet("/")
public class IndexServlet extends HttpServlet {
    private EmployeeService service=new EmployeeServiceImpl();
    private static Logger logger= LoggerFactory.getLogger(IndexServlet.class);

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 将request请求中获取url参数，并构建为GoodsInfoDto的对象
        // GoodsInfoDto对象中包含了分页查询的条件和页码等参数
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        EmployeeDto goodsInfoDto= ServletUtil.parameter2dto(req,EmployeeDto.class);
        R r=service.selectAll(goodsInfoDto);

       // 得到pageInfo,包含了页码，pageSize,total,list
        Object pageInfo=r.getData();

        // 将pageVo保存到request作用域中
        // 再做一次页面转发，将request请求转发到另外的用于展示数据的jsp页面中
        // jsp页面只复制遍历，展示数据

        req.setAttribute("pageInfo",pageInfo);

         // 用户不能够在浏览器中访问到WEB-INF目录中的文件
        RequestDispatcher requestDispatcher=req.getRequestDispatcher("/WEB-INF/pages/index.jsp");
        // 页面转发


        requestDispatcher.forward(req,resp);
        // 记： 页面转发是一次发生在服务器内部的行为
        // 浏览器中请求的url地址不会变化，用户不能感知到服务器在内部多个servlet之间跳转了多少次
        // 用户请求：/goods/search 这个servlet
        // 实际上用户看到的页面来自于/WEB-INF/pages/goodsInfo.jsp

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       //   DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
       //   EmployeeService service=new EmployeeServiceImpl();


       //  logger.debug("reg servlet接收到客户端发起Post请求");
       //  req.setCharacterEncoding("utf-8");
       //  resp.setCharacterEncoding("utf-8");

       //  /*type为2表示修改 - 自定义修改*/
       // int type=Integer.parseInt(req.getParameter("type"));
       //  if(type==3/*删除*/){
       //      int id=Integer.parseInt(req.getParameter("id"));
       //      logger.debug("reg servlet接收到客户端发起删除ID={}",id);
       //      R r= service.deleteByPrimaryKey(id);
       //      System.out.println(r.getMessage());
       //  }
       //  if(type==2/*修改*/ || type==1/*新增*/){
       //      Employee employee=new Employee();
       //      if(!req.getParameter("id").equals("*")){
       //        employee.setId(Integer.parseInt(req.getParameter("id")));
       //      }

       //      employee.setName(req.getParameter("name"));
       //      employee.setUsername(req.getParameter("username"));
       //      employee.setPhone(req.getParameter("phone"));
       //      Date date=null;
       //      try {
       //          date=dateFormat.parse((req.getParameter("entryTime")+""));
       //       }catch (ParseException e){
       //         throw new RuntimeException(e);
       //      }
       //      employee.setEntryTime(date);
       //      employee.setStatus(Integer.parseInt(req.getParameter("status")));
       //      employee.setPosition(Integer.parseInt(req.getParameter("position")));
       //      /*所属部门*/
       //      employee.setOfDepartment(Integer.parseInt(req.getParameter("ofDepartment")));
       //      logger.debug("您传入修改的员工信息{}",employee);
       //      if(type==2/*修改*/){
       //       R r=service.updateByPrimaryKeySelective(employee);
       //       System.out.println(r.getMessage());
       //      }
       //      if(type==1/*新增*/){
       //          employee.setUpdateTime(new Date());
       //          employee.setPassword("123456");
       //          R r=service.insert(employee);
       //          System.out.println(r.getMessage());
       //      }

       //  };
    }
}
